<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page,useVbenDrawer } from '@vben/common-ui';
import { Button,Popconfirm,message } from 'ant-design-vue';
import { IconifyIcon } from '@vben/icons';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getListApi,deleteApi } from '#/api/system/agreement';
import Form from './form.vue';

interface RowType {
  id: number;
  name: string;
  title: string;
  remark: string;
}

const gridOptions: VxeGridProps<RowType> = {
  columns: [
    { field: 'name', title: '名称', minWidth:'auto' },
    { field: 'title', title: '标题', minWidth:'auto' },
    { field: 'remark', title: '备注', minWidth:'auto' },
    { field: 'action', fixed: 'right', slots: { default: 'action' }, title: '操作', minWidth:'auto' },
  ],
  showOverflow: false,
  exportConfig: {},
  keepSource: true,
  stripe: false,
  // border: true,
  proxyConfig: {
    ajax: {
      query: async () => {
        return await getListApi();
      },
    },
    sort: true,
  },
  toolbarConfig: {
    custom: true,
    // export: true,
    // import: true,
    refresh: { code: 'query' },
    zoom: true,
  },
  pagerConfig: {
    enabled: false,
  },
  rowConfig: {
    keyField: 'id',//主键的字段名
  },
};

const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
  onBeforeClose() {
    gridApi.query();
    return true;
  },
});
function openFormDrawer(item={id:0}) {
  formDrawerApi
    .setData(item)
    .open();
}
function confirmDel(id: number) {
  gridApi.setLoading(true);
  deleteApi(id).then((res) => {
    gridApi.setLoading(false);
    message.success({
      content: res.message,
    });
    gridApi.query();
  });
}
</script>

<template>
  <Page>
    <Grid table-title="协议条款">
      <template #toolbar-tools>
        <Button class="mr-2" type="primary" shape="round" @click="openFormDrawer()"> <div class="flex items-center justify-center"><IconifyIcon class="size-4 cursor-pointer outline-none mr-1" icon="ant-design:plus-circle-filled" />新增协议</div> </Button>
      </template>
      <template #action="{ row }">
        <Button type="link" class="px-2" @click="openFormDrawer(row)"><IconifyIcon class="size-4 cursor-pointer outline-none" icon="ant-design:edit-outlined" /></Button>
        <Button type="link" class="px-0 mx-2" danger>
          <popconfirm title="确认删除此协议?" ok-text="确认" cancel-text="取消" placement="left" @confirm="confirmDel(row.id)">
            <template #icon>
              <IconifyIcon class="size-4 text-red-600 outline-none inline" icon="ant-design:question-circle-outlined" />
            </template>
            <IconifyIcon class="size-4 cursor-pointer outline-none" icon="ant-design:delete-outlined" />
          </popconfirm>
        </Button>
      </template>
    </Grid>
    <FormDrawer />
  </Page>
</template>
